﻿using TrueSync;
using UnityEngine;

namespace HEFramework
{
    public static class FactorySystem
    {
        [EntitySystem]
        public class FactoryAwakeSystem : AwakeSystem<Factory>
        {
            protected override void Awake(Factory _self)
            {
            }
        }

        public static Entity AddHero(this Factory _self, int _id, int _teamID, TSVector _pos)
        {
            var hero = _self.GetScene().AddChild<Hero, int>(_id);
            var ac = hero.GetChild<AttributeComponent>();
            var tc = hero.GetChild<TransformComponent>();

            if (ac != null)
            {
                ac.Change_T_ID(_teamID);
            }

            if (tc != null)
            {
                tc.P = _pos;
            }

            return hero;
        }

        public static Entity AddMonster(this Factory _self, int _id, int _teamID, TSVector _pos)
        {
            var monster = _self.GetScene().AddChild<Monster, int>(_id);
            var ac = monster.GetChild<AttributeComponent>();
            var tc = monster.GetChild<TransformComponent>();

            if (ac != null)
            {
                ac.Change_T_ID(_teamID);
            }

            if (tc != null)
            {
                tc.P = _pos;
            }

            return monster;
        }

        public static Entity AddBuilding(this Factory _self, int _id, int _teamID, TSVector _pos)
        {
            var building = _self.GetScene().AddChild<Building, int>(_id);
            var ac = building.GetChild<AttributeComponent>();
            var tc = building.GetChild<TransformComponent>();
            if (ac != null)
            {
                ac.Change_T_ID(_teamID);
            }

            if (tc != null)
            {
                tc.P = _pos;
            }

            return building;
        }

        public static TSVector GetRandomHeroGeneratePos(this Factory _self)
        {
            //随机位置
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return new TSVector();
            }

            var gTable = dc.LoadDataTable<DRGlobalSettings>();
            using var entitys = _self.GetScene().GetChildList<Entity>();
            FP range = Formula.LG(gTable.GetData((int)eGlobalSetting.Hero_Generate_RAG).Value1);
            TSVector pos;
            FP angle = Random.Range(0, 360);
            pos = _self.AngleToDirection(angle) * range;
            return pos;
        }

        public static TSVector GetRandomMonsterGeneratePos(this Factory _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return new TSVector();
            }

            var gTable = dc.LoadDataTable<DRGlobalSettings>();
            //随机坐标
            FP range = Formula.LG(gTable.GetData((int)eGlobalSetting.Monster_Generate_RAG).Value1);
            FP angle = Random.Range(0, 360);
            return _self.AngleToDirection(angle) * range;
        }


        public static TSVector AngleToDirection(this Factory _self, FP _angle)
        {
            // 角度转换为弧度
            FP angleInRadians = _angle * FP.Deg2Rad;
            // 计算朝向的向量
            TSVector2 direction = new TSVector2(FP.Cos(angleInRadians), FP.Sin(angleInRadians));
            return new TSVector(direction.x, direction.y, 0);
        }
    }
}